package com.zx.idc.backend.gui.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.shiro.TokenManagerUtil;
import com.zx.idc.backend.gui.sys.service.ISysBackupLogJobService;
import com.zx.idc.backend.gui.sys.service.SysBackupLogServiceOfFtp;
import com.zx.idc.ds.sys.bean.DataBackupSetting;
import com.zx.idc.ds.sys.bean.OffSiteBackupSetting;
import com.zx.idc.ds.sys.entity.SysBackupLog;
import com.zx.idc.ds.sys.entity.SysConfiguration;
import com.zx.idc.ds.sys.service.ISysBackupLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 系统备份记录表 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-04-13
 */
@RestController
@RequestMapping("/api/sysBackupLog")
public class SysBackupLogController extends BasicBaseApi<ISysBackupLogService, SysBackupLog> {
    @Autowired
    private TokenManagerUtil tokenManagerUtil;
    @Autowired
    private ISysBackupLogJobService sysBackupLogJobService;
    @Autowired
    private SysBackupLogServiceOfFtp sysBackupLogServiceOfFtp;

    @GetMapping("/query-page")
    @RequiresPermissions("system-configuration:data-backup-setting")
    public Result<Page<SysBackupLog>> pageByParam(Page<SysBackupLog> page, SysBackupLog entity) {
        return success(service.pageByParam(page, entity));
    }

    @GetMapping("/get-settings")
    @RequiresPermissions("system-configuration:data-backup-setting")
    public Result<Map<String, SysConfiguration>> getSettings() {
        return success(service.getSettings());
    }

    @PostMapping("/init-settings")
    @RequiresPermissions("system-configuration:data-backup-setting:edit")
    public Result<Boolean> initSettings() {
        return success(service.updateInitSettings(tokenManagerUtil.getCurrentUser().getId()));
    }

    @PostMapping("/update-settings")
    @RequiresPermissions("system-configuration:data-backup-setting:edit")
    public Result<Boolean> updateSettings(DataBackupSetting dataBackupSetting, OffSiteBackupSetting offSiteBackupSetting) {
        return success(sysBackupLogJobService.updateSettings(dataBackupSetting, offSiteBackupSetting, tokenManagerUtil.getCurrentUser().getId()));
    }

    @PostMapping("/manual-backup")
    @RequiresPermissions("system-configuration:data-backup-setting:edit")
    public Result<Boolean> manualBackup(DataBackupSetting dataBackupSetting, OffSiteBackupSetting offSiteBackupSetting) {
        return success(sysBackupLogServiceOfFtp.addManualBackup(dataBackupSetting, offSiteBackupSetting, tokenManagerUtil.getCurrentUser().getId()));
    }

    @PostMapping("/delete-backup/{id}")
    @RequiresPermissions("system-configuration:data-backup-setting:edit")
    public Result<Boolean> deleteBackup(@PathVariable Integer id) {
        return success(service.deleteBackup(id));
    }

    @PostMapping("/restore/{id}")
    @RequiresPermissions("system-configuration:data-backup-setting:edit")
    public Result<Boolean> restore(@PathVariable Integer id) {
        return success(service.updateRestore(id));
    }

}

